<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\InsureType as InsureTypeModel;

class InsureType extends BaseController{
    public function __construct(InsureTypeModel $roleModel){
        parent::__construct();
        $this->model = $roleModel;
    }
    
    public function index(){
        $params = $this->request->param();
        $data = $this->model->allowField(true)->roleList($params);
        return $data;
    }
    //添加
    public function create(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);    
        if(!empty($params['purviews'])){
            $params['purviews'] = implode(',',$params['purviews']);
        }    
        $data = $this->model->allowField(true)->data($params)->save();
        if($data){
            Logs::add("新增角色记录",$operator_id,"角色", null,json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $this->model;
    }
     //查询指定数据
     public function read(){        
        $params = $this->request->param();        
        if(empty($params['id'])){
            return '缺少id参数';
        }      
        
        $data = $this->model->where('id',$params['id'])->find();
        return $data ;
    }
    //更新
    public function update(){
        $param = $this->request->param();       
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);    
        $params['purviews'] = implode(',',$params['purviews']);
        $old = $this->model->where("id",$params['id'])->find(); 
        $data = $this->model->allowField(true)->save($params,['id',$params['id']]);
        if($data){
            Logs::add("修改角色记录",$operator_id,"角色", json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $this->model;
    }
    //删除
    public function delete(){
        $params = $this->request->param();
        $old = $this->model->where("id",$params['id'])->find();   
        $data = $this->model->allowField(true)->where('id',$params['id'])->delete();
        if($data){
            Logs::add("删除角色记录",$params['operator_id'],"角色", json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $data;
    }
}
